﻿using mediaControl.Event;
using mediaControl.Utils;
using mediaControl.Views;
using mediaControl.Vo;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using MessageBox = System.Windows.MessageBox;

namespace mediaControl.ViewModels
{
    public class NewProgramViewModel : BindableBase,IDialogAware
    {

        
        //事件聚合器
        private readonly IEventAggregator  eventAggregator;
        public NewProgramViewModel(IEventAggregator eventAggregator)
        {
            closeCommand = new DelegateCommand(new Action(Close));

            getFilePathCommand = new DelegateCommand(new Action(getFilePath));

            createFileCommand = new DelegateCommand(new Action(creatFile));
            closeCommand = new DelegateCommand(new Action(closeWindow));

            this.eventAggregator = eventAggregator;

        }

        private String fileFullPath;

        public String Title { set; get; }

        private String filePath = "";

        private String programName = "";

        public event Action<IDialogResult> RequestClose;

        public String ProgramName { 
            get { return programName; }
            set { programName = value; RaisePropertyChanged();}
       }

      public String FilePath {
            get { return filePath; }
            set { filePath = value; RaisePropertyChanged();}
        }

        public DelegateCommand closeCommand { set; get; }
        /// <summary>
        /// 打开文件获取路径
        /// </summary>
        public DelegateCommand getFilePathCommand { set; get; }
        /// <summary>
        /// 保存文件
        /// </summary>
        public DelegateCommand createFileCommand { set; get; }


        private void Close() {
            
        }
        /// <summary>
        /// 打开文件获取路径
        /// </summary>
        private void getFilePath() {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.ShowDialog();        //这个方法可以显示文件夹选择对话框
            this.FilePath = folderBrowserDialog.SelectedPath;    //获取选择的文件夹的全路径名
        }

        private void creatFile() {
            if(String.IsNullOrEmpty(this.FilePath) || String.IsNullOrEmpty(this.ProgramName)){
                MessageBox.Show("名称和路径不能为空！");
                return;
            }
            fileFullPath = this.FilePath+"\\"+this.programName+".info"; // 指定要创建的文件路径及名称
            try
            {
                if (File.Exists(fileFullPath)) {
                    MessageBoxResult result = MessageBox.Show("是否覆盖？","文件存在提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        // 覆盖文件
                        //保存节目信息
                        ProgramVo programNew = new ProgramVo(0, programName, FilePath);
                        string outputJSONNew = JsonConvert.SerializeObject(programNew);
                        JObject jsonObjectNew = new JObject();
                        jsonObjectNew.Add("programInfo", outputJSONNew);
                        WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(jsonObjectNew), "2");
                        //通知列表显示
                        IDialogParameters parameters1 = new DialogParameters();
                        parameters1.Add("program", programNew);
                        eventAggregator.GetEvent<ProgramEvent>().Publish(programNew);
                        RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, parameters1));

                    }
                    else
                    {
                        
                    }
                    return;
                }
                
                //保存节目信息
                ProgramVo program = new ProgramVo(0, programName, FilePath);
                string outputJSON = JsonConvert.SerializeObject(program);
                JObject jsonObject = new JObject();
                jsonObject.Add("programInfo", outputJSON);
                WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(jsonObject), "2");
                //通知列表显示
                IDialogParameters parameters = new DialogParameters();
                parameters.Add("program", program);
                eventAggregator.GetEvent<ProgramEvent>().Publish(program);
                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, parameters));
                WriteFileUtil.SaveOpenList(fileFullPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"无法创建文件：{ex.Message}");
                MessageBox.Show($"无法创建文件：{ex.Message}");
                return;
            }
            
        }

        private void closeWindow() {
            RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.No));
        }

        public bool CanCloseDialog()
        {
            //throw new NotImplementedException();
            return true;
        }

        public void OnDialogClosed()
        {
            

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            this.Title = "新建节目";
        }
    }
}
